from service.MongoService import MongoTable

name = 'users'

records = dict()
table = MongoTable(name)

OPENID = 'openid'
DEVICES = 'devices'
DEVICES_NAME = 'name'


def get_nick_name(open_id, device_id):
    if open_id not in records:
        data = {
            OPENID: open_id
        }
        user_info = table.find_one(data)
        records[open_id] = user_info

    user_info = records[open_id]

    devices = user_info[DEVICES]

    for tmp_device_id in devices:
        if tmp_device_id == device_id:
            return devices[tmp_device_id][DEVICES_NAME]

    return ''
